﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DDD.Core;
using FloatComparer = DDDTest.Helper.FloatComparer;

namespace DDDTest.Core {
    [TestClass]
    public class TestSampler {

        [TestMethod]
        public void Test_New1 () {
            var tex = new Sampler (SamplerType.Texture2D);

            Assert.AreEqual (SamplerType.Texture2D, tex.Type);
            Assert.AreEqual (1, tex.ImageCount);
            Assert.AreEqual (null, tex.Image);
            Assert.AreEqual (ImageFilter.Linear, tex.ImageFilter);
            Assert.AreEqual (LevelFilter.Linear, tex.LevelFilter);
            Assert.AreEqual (TextureWrapMode.Repeat, tex.WrapMode);
        }

        [TestMethod]
        public void Test_SetImage_1 () {
            var smp = new Sampler (SamplerType.Texture2D);
            var img = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
            smp.SetImage (img);

            Assert.AreEqual (1, smp.ImageCount);
            Assert.AreEqual (img, smp.Image);
        }

        [TestMethod]
        public void Test_SetImage_2 () {
            var sampler = new Sampler (SamplerType.TextureCube);
            var negX = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
            var posX = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
            var negY = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
            var posY = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
            var negZ = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
            var posZ = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);

            sampler.SetImage (CubeFace.NegativeX, negX);
            sampler.SetImage (CubeFace.PositiveX, posX);
            sampler.SetImage (CubeFace.NegativeY, negY);
            sampler.SetImage (CubeFace.PositiveY, posY);
            sampler.SetImage (CubeFace.NegativeZ, negZ);
            sampler.SetImage (CubeFace.PositiveZ, posZ);

            Assert.AreEqual (6, sampler.ImageCount);
            Assert.AreEqual (negX, sampler.GetImage (CubeFace.NegativeX));
            Assert.AreEqual (posX, sampler.GetImage (CubeFace.PositiveX));
            Assert.AreEqual (negY, sampler.GetImage (CubeFace.NegativeY));
            Assert.AreEqual (posY, sampler.GetImage (CubeFace.PositiveY));
            Assert.AreEqual (negZ, sampler.GetImage (CubeFace.NegativeZ));
            Assert.AreEqual (posZ, sampler.GetImage (CubeFace.PositiveZ));
        }


        [TestMethod]
        public void Test_SetImages () {
            var smp = new Sampler (SamplerType.TextureCube);
            var imgs = new Image2D<byte>[6]{ new Image2D<byte>(PixelFormat.RGB8ui, 16, 16),
                                            new Image2D<byte>(PixelFormat.RGB8ui, 16, 16),
                                            new Image2D<byte>(PixelFormat.RGB8ui, 16, 16),
                                            new Image2D<byte>(PixelFormat.RGB8ui, 16, 16),
                                            new Image2D<byte>(PixelFormat.RGB8ui, 16, 16),
                                            new Image2D<byte>(PixelFormat.RGB8ui, 16, 16)};
            smp.SetImages (imgs);
            var array = smp.Images.ToArray ();

            Assert.AreEqual (6, smp.ImageCount);
            CollectionAssert.AreEqual (imgs, array);
        }



        [TestMethod]
        public void Test_Eumerator () {
            var imgs = new Image2D[]{new Image2D<byte>(PixelFormat.RGB8ui, 16,16),
                                     new Image2D<byte>(PixelFormat.RGB8ui, 16,16),
                                     new Image2D<byte>(PixelFormat.RGB8ui, 16,16),
                                     null, 
                                     new Image2D<byte>(PixelFormat.RGB8ui, 16,16),
                                     new Image2D<byte>(PixelFormat.RGB8ui, 16,16)};
            var tex = new Sampler (SamplerType.TextureCube);
            tex.SetImages (imgs);

            var sum = tex.Images.Sum (x => 1);

            Assert.AreEqual (5, sum);
        }

        [TestMethod]
        public void Test_SetFilter () {
            var tex = new Sampler (SamplerType.Texture2D);

            tex.SetFilter (ImageFilter.Nearest, LevelFilter.Base);

            Assert.AreEqual (ImageFilter.Nearest, tex.ImageFilter);
            Assert.AreEqual (LevelFilter.Base, tex.LevelFilter);
        }

        [TestMethod]
        public void Test_SetWrapMode () {
            var tex = new Sampler (SamplerType.Texture2D);

            tex.SetWrapMode (TextureWrapMode.Clamp);

            Assert.AreEqual (TextureWrapMode.Clamp, tex.WrapMode);
        }

        [TestMethod]
        public void Test_References () {
            var img = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
            var tex = new Sampler (SamplerType.Texture2D);
            tex.SetImage (img);

            var objs = tex.References;

            Assert.AreEqual (1, objs.Count ());
            Assert.AreEqual (true, objs.All (x => x != null));
        }

        [TestMethod]
        public void Test_Find () {
            var img = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
            var tex = new Sampler (SamplerType.Texture2D);
            tex.SetImage (img);

            img.UserID = 101;
            tex.UserID = 102;

            Assert.AreEqual (img, tex.Find (101));
            Assert.AreEqual (tex, tex.Find (102));
        }
    }
}
